package com.cn.utils;

import com.cn.entity.Department;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
 * 生成部门树
 * * @param departmentList  部门列表
 * * @param pid             父级部门ID
 * * @return
 */
public class DepartmentTree {

        public static List<Department> makeDepartmentTree(List<Department>
                                                                  departmentList, Long pid) {
            List<Department> list = new ArrayList<Department>();
            //如果deptList部门列表不为空，则使用部门列表，否则创建集合对象
            Optional.ofNullable(departmentList).orElse(new ArrayList<Department>())
                    .stream().filter(item -> item != null && item.getPid() == pid)
                    .forEach(item -> {
                        //创建部门对象
                        Department dept = new Department();
                        //复制属性
                        BeanUtils.copyProperties(item, dept);
                        //获取每一个item的下级部门,递归生成部门树
                        List<Department> children =
                                makeDepartmentTree(departmentList, item.getId());
                        //设置子部门
                        dept.setChildren(children);
                        //将部门对象添加到集合
                        list.add(dept);
                    });
            //返回数据
            return list;
        }
    }
